package com.qf.commerce.handler;

import com.qf.commerce.exception.BaseException;
import com.qf.commerce.exception.ControllerException;
import com.qf.commerce.exception.DaoException;
import com.qf.commerce.exception.ServiceException;
import com.qf.commerce.resp.ResponseResult;
import com.qf.commerce.resp.ResponseStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalHandlerException {
    @ExceptionHandler(Exception.class)
    public ResponseResult<Object> handlerEx(Exception e){
        System.out.println(e.getMessage());
        return ResponseResult.fail(ResponseStatus.SYS_ERROR);
    }

    //多个处理参数的方法，会先找小异常找不到最终才找大异常
    //打印给开发人员看异常信息，前端看返回值
    @ExceptionHandler(BaseException.class)
    public ResponseResult<Object> handlerEx(BaseException be){
        System.out.println(be.getMessage());
        return ResponseResult.fail(be.getResponseStatus());
    }

    @ExceptionHandler(ControllerException.class)
    public ResponseResult<Object> handlerEx(ControllerException c){
        System.out.println(c.getMessage());
        return ResponseResult.fail(c.getResponseStatus());
    }

    @ExceptionHandler(DaoException.class)
    public ResponseResult<Object> handlerEx(DaoException d){
        System.out.println(d.getMessage());
        return ResponseResult.fail(d.getResponseStatus());
    }

    @ExceptionHandler(ServiceException.class)
    public ResponseResult<Object> handlerEx(ServiceException s){
        System.out.println(s.getMessage());
        return ResponseResult.fail(s.getResponseStatus());
    }
}
